package com.example.mvvm

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.mvvm.base.Default
import com.example.mvvm.base.Empty
import com.example.mvvm.base.Failed
import com.example.mvvm.base.Loading
import com.example.mvvm.base.Success
import com.example.mvvm.base.observeWithLifecycle
import com.example.mvvm.viewmodel.MainViewModel
import kotlinx.coroutines.launch
const val TAG = "MainActivity"
class MainActivity : AppCompatActivity() {
    private val mainViewModel:MainViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        //流的扩展函数
        mainViewModel.data3.observeWithLifecycle(this){
            when(it){
                        Default -> Log.d(TAG, "onCreate: default")
                        Empty -> Log.d(TAG, "onCreate: Empty")
                        is Failed ->  Log.d(TAG, "onCreate: Failed"+it.message)
                        Loading ->  Log.d(TAG, "onCreate: Loading")
                        Success -> Log.d(TAG, "onCreate: Success")
                    }
        }
//        //订阅数据
//        lifecycleScope.launch {
//            repeatOnLifecycle(Lifecycle.State.STARTED){
////                mainViewModel.data1.observe(this@MainActivity){
////                    Log.d(TAG, "onCreate1: "+it.size)
////                }
////
////                mainViewModel.data2.collect{
////                    Log.d(TAG, "onCreate2: "+it.size)
////                }
//
//                mainViewModel.data3.collect{it->
//                    when(it){
//                        Default -> Log.d(TAG, "onCreate: default")
//                        Empty -> Log.d(TAG, "onCreate: Empty")
//                        is Failed ->  Log.d(TAG, "onCreate: Failed"+it.message)
//                        Loading ->  Log.d(TAG, "onCreate: Loading")
//                        Success -> Log.d(TAG, "onCreate: Success")
//                    }
//                }
//
//            }
//        }
    }
    //发起网络请求
    fun getData(view: View) {
        val params = mutableMapOf<String, String>()
        params["currentPage"] = "1"
        params["pageSize"] = "20"
        mainViewModel.getVideoList(params)
    }
}